using System.Security.Cryptography;
using System.Text;

#pragma warning disable SYSLIB0021

namespace Bzstd.Utility;

/// <summary>
/// AES工具类
/// </summary>
public static class AesHelper
{
	/// <summary>
	/// 密钥
	/// </summary>
	public static readonly byte[] key = "E65F9A2EA36148BEB2837FBAB2E171E2"u8.ToArray();

	/// <summary>
	/// AES加密
	/// </summary>
	/// <param name="str"></param>
	/// <returns></returns>
	public static string Encrypt(string str)
	{
		using var aesProvider = new AesCryptoServiceProvider
		{
			Key = key, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7
		};
		using var cryptoTransform = aesProvider.CreateEncryptor();
		var inputBuffer = Encoding.UTF8.GetBytes(str);
		var result = cryptoTransform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
		aesProvider.Clear();
		return Convert.ToHexString(result);
	}

	/// <summary>
	/// AES解密
	/// </summary>
	/// <param name="str"></param>
	/// <returns></returns>
	public static string Decrypt(string str)
	{
		using var aesProvider = new AesCryptoServiceProvider
		{
			Key = key, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7
		};
		using var cryptoTransform = aesProvider.CreateDecryptor();
		var inputBuffer = Convert.FromHexString(str);
		var result = cryptoTransform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
		aesProvider.Clear();
		return Encoding.UTF8.GetString(result);
	}

#if DEBUG
	public static void TestEncrypt()
	{
		string text = Encrypt("pwdpwD!@#12345");
		// Clipboard.SetText(text);
		Console.WriteLine(text);
		// MessageBox.Show(text + "\n\n已复制到剪切板");
	}

	public static void TestDecrypt()
	{
		string text = Decrypt("MbnJ3v57Q9q7zt0PS4avjQ==");
		// Clipboard.SetText(text);
		Console.WriteLine(text);
		// MessageBox.Show(text + "\n\n已复制到剪切板");
	}
#endif
}
